#include"Queue.h"


void QueueInit(Que* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}
QNode* BuyQueueNode(QueueDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("BuyQueueNode->malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void QueuePush(Que* pq, QueueDataType x)
{
	assert(pq);
	QNode* newnode = BuyQueueNode(x);
	if (pq->head==NULL)
	{
		pq->tail = pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Que* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	if (pq->head == pq->tail)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* del = pq->head;
		pq->head = pq->head->next;
		free(del);
	}
}
QueueDataType QueueFront(Que* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	return pq->head->data;
}
QueueDataType QueueBack(Que* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	return pq->tail->data;
}
bool QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->head == NULL;
}
void QueueDestory(Que* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur !=NULL)
	{
		QNode* curNext = cur->next;
		free(cur);
		cur = curNext;
	}
}

